const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    settlementOfTheCar: [], // 购物车
    sum: 0, // 合计
    datas: [], // 用户选择的店铺信息
    site: [], // 外卖地址
    fun: -1 // 自取还是外卖
  },
  onLoad: function (data) {
    this.setData({
      fun: data.fun
    })
    this.setSettlementOfTheCar()
    this.setDatas(data.fun)
    this.sites()
  },
  sites() {
    this.setData({
      sites: app.globalData.user.site
    })
  },
  setDatas(fun) {
    if (fun == 0) {
      this.setData({
        datas: app.globalData.datasTack
      })
    }
    if (fun == 1) {
      this.setData({
        datas: app.globalData.datas
      })
    }
  },
  setSettlementOfTheCar() {
    this.setData({
      settlementOfTheCar: app.globalData.user.settlementOfTheCar
    })
    let sumOne = 0;
    for (let a = 0; a < this.data.settlementOfTheCar.length; a++) {
      sumOne = this.data.settlementOfTheCar[a].Price + sumOne
    }
    this.setData({
      sum: sumOne
    })
  },
  submits() {
    let fun = -1; // 外卖或者自取 1自取 0外卖
    if (this.data.fun == 1) {
      fun = "到店自取"
    } else {
      fun = "外卖配送"
    }

    let shop = app.globalData.user.settlementOfTheCar
    for (let a = 0; a < shop.length; a++) {
      let data = Math.round(Math.random() * 100000000000) // 随机数
      var myDate = new Date();
      var mytime = myDate.toLocaleTimeString(); //获取当前时间
      let item = { // 要传的值
        orderNo: "" + data,
        wxOpenid: app.globalData.openid,
        orderStatus: "制作中",
        takeType: fun,
        addressDetail: this.data.datas.name,
        goodsPreview: shop[a].name,
        finishTime: mytime,
        userPhone: app.globalData.user.site.shopPhone
      }
      wx.request({
        url: `http://localhost:8081/interfaced/info/insert`,
        data: {
          ...item // 传值
        }
      })
    }
    app.globalData.user.money = app.globalData.user.money - this.data.sum
    wx.reLaunch({
      url: '/pages/homepage/index',
    })
  }
})